import cgi
import os

from TopicDao import *

from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class AdminPage(webapp.RequestHandler):
    def get(self):    
        topicDao = TopicDao()
        topics = topicDao.loadAll()
        
        user = users.get_current_user()
        
        isAdmin = users.is_current_user_admin()
        
        loginUrl = users.create_login_url(self.request.uri)

        template_values = {
                           'topics': topics,
                           'isAdmin': isAdmin,
                           'loginUrl': loginUrl,
                           }

        path = os.path.join(os.path.dirname(__file__), 'admin.html')
        self.response.out.write(template.render(path, template_values))
        

class DeleteTopic(webapp.RequestHandler):
    def post(self):
        topic_query = Topic.gql("WHERE content = :1", self.request.get('content'))
        topic = topic_query.fetch(1)
        topic[0].delete()
        self.redirect('/admin')
